package com.central.scheduler.service.queue.entity;

import com.central.common.enums.TaskType;
import com.central.common.model.ResourceInfo;
import com.central.scheduler.spi.task.request.DataxTaskExecutionContext;
import com.central.scheduler.common.entity.DependenceTaskExecutionContext;
import com.central.scheduler.common.enums.ExecutionStatus;
import com.central.scheduler.common.enums.TaskTimeoutStrategy;
import com.central.common.properties.Property;import com.central.scheduler.common.utils.JSONUtils;
import com.central.scheduler.remote.command.Command;
import com.central.scheduler.remote.command.TaskExecuteRequestCommand;
import com.central.scheduler.spi.task.request.FlinkSqlTaskExecutionContext;
import com.central.scheduler.spi.task.request.SQLTaskExecutionContext;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Tindy
 * @date 2021/10/29
 * @describe
 * master/worker task transport
 */
@Getter
@Setter
@ToString
public class TaskExecutionContext implements Serializable {
    /**
     * task id
     */
    private Long taskInstanceId;

    /**
     * task name
     */
    private String taskName;

    /**
     * task first submit time.
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date firstSubmitTime;

    /**
     * task start time
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date startTime;

    /**
     * task type
     */
    private TaskType taskType;

    /**
     * host
     */
    private String host;

    /**
     * task execute path
     */
    private String executePath;

    /**
     * log path
     */
    private String logPath;

    /**
     * processId
     */
    private int processId;

    /**
     * processCode
     */
    private Long processDefineCode;

    /**
     * processVersion
     */
    private int processDefineVersion;

    /**
     * appIds
     */
    private String appIds;

    /**
     * process instance id
     */
    private Long processInstanceId;


    /**
     * process instance schedule time
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date scheduleTime;

    /**
     * task instance local parameters
     */
    private List<Property> localParams;
    /**
     * process instance global parameters
     */
    private List<Property> globalParams;


    /**
     * execute user id
     */
    private Long userId;
    private String username;


    /**
     * command type if complement
     */
    private int cmdTypeIfComplement;

    /**
     * task queue
     */
    private String queue;

    /**
     * project code
     */
    private long proId;
    private String proName;

    /**
     * envFile
     */
    private String envFile;

    /**
     * environmentConfig
     */
    private String environmentConfig;


    /**
     * definedParams
     */
    private Map<String, String> definedParams;

    /**
     * task AppId
     */
    private String taskAppId;

    /**
     * task timeout strategy
     */
    private TaskTimeoutStrategy taskTimeoutStrategy;

    /**
     * task timeout
     */
    private int taskTimeout;

    /**
     * worker group
     */
    private Long workerGroupId;

    /**
     * delay execution time.
     */
    private int delayTime;

    /**
     * current execution status
     */
    private ExecutionStatus currentExecutionStatus;

    private List<ResourceInfo> resources;

    private String taskContent;

    /**
     * sql TaskExecutionContext
     */
    private SQLTaskExecutionContext sqlTaskExecutionContext;

    /**
     * datax TaskExecutionContext
     */
    private DataxTaskExecutionContext dataxTaskExecutionContext;

    /**
     * flink sql TaskExecutionContext
     */
    private FlinkSqlTaskExecutionContext flinkSqlTaskExecutionContext;

    /**
     * dependence TaskExecutionContext
     */
    private DependenceTaskExecutionContext dependenceTaskExecutionContext;


    /**
     * taskInstance varPool
     */
    private List<Property> varPool;

    /**
     * dry run flag
     */
    private int dryRun;

    /**
     * business param
     */
    private Map<String, Property> paramsMap;
    public Command toCommand() {
        TaskExecuteRequestCommand requestCommand = new TaskExecuteRequestCommand();
        requestCommand.setTaskExecutionContext(JSONUtils.toJsonString(this));
        return requestCommand.convert2Command();
    }

}
